<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>服务类型配置</title>
    <header th:replace="header::html"></header>
</head>
<body>
<input type="button" id="add" class="layui-btn" value="新增类型">


<table id="demo" lay-filter="test"></table>

<script type="text/html" id="bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">删除</a>
</script>

<script>

	layui.use('table', function(){
		var table = layui.table;
		table.render({
			elem: '#demo'
			,type:'POST'
			,url: '/api-back/type/getOtherList'
			,page: true
			,limit:5
			,limits:[5]
			,cols: [
				[
					{field: 'sid', hide:true}
					,{field: 'sname', title: '类型名称', width:200}
					,{title:'操作',width:150,toolbar:"#bar"}
				]
			]
		});


		table.on('tool(test)', function(obj)
		{
			var data = obj.data;
			var layEvent = obj.event;
			var tr = obj.tr;
			if(data.sname==='其他')
			{
				layer.alert("该类型无法删除，请重试", {icon: 5});
			}
			else
			{
				layer.confirm('确定删除该类型吗?', function(index)
				{
					$.ajax({
						type:"POST",
						url:'/api-back/type/deleteOtherType',
						dataType:"text",
						data:{sid:data.sid,sname:data.sname},
						success:function(msg)
						{
							if(msg==="yes")
							{
								layer.alert("删除成功", {icon: 1});
								$(".layui-laypage-btn")[0].click();
								layer.close(index);
							}
							else
							{
								layer.alert("目前类型数量较少，请先新增", {icon: 5});
							}
						},
						error:function ()
						{
							layer.alert("服务器正忙.....", {icon: 5});
						}
					});
				});
			}
		});

		$('#add').click(function ()
		{
			layer.prompt({title:'请输入类型名称'},function(value, index, elem)
			{
				if(value.length===0)
				{
					layer.alert("请输入内容", {icon: 5});
				}
				else if(value.length>6)
				{
					layer.alert("类型名称不得超过6个字", {icon: 5});
				}
				else
				{
					$.ajax({
						type:"POST",
						url:"/api-back/type/checkOther",
						dataType:"text",
						data:{sname:value},
						success:function(msg)
						{
							if(msg==="yes")
							{
								$.ajax({
									type:"POST",
									url:"/api-back/type/addOtherType",
									dataType:"text",
									data:{sname:value},
									success:function(msg)
									{
										if(msg==="yes")
										{
											layer.open({
												title: ['温馨提示'],
												content: '新增成功',
												btn: ['确定'],
												closeBtn :0,
												yes: function(){
													window.location.href="/api-back/getPage?pageName=backPage/otherType";
												}
											});
										}
										else
										{
											layer.alert("新增失败，请重试", {icon: 5});
										}
									},
									error:function ()
									{
										layer.alert("服务器正忙.....", {icon: 5});
									}
								});
							}
							else
							{
								layer.alert("该类型已存在", {icon: 5});
							}
						},
						error:function ()
						{
							layer.alert("服务器正忙.....", {icon: 5});
						}
					});
				}
			});
		});

	});
</script>
</body>
</html>